package com.example.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.video.entity.OuterAccount;
import com.example.video.mapper.OuterAcountMapper;
import com.example.video.service.IOuterAccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qiusuyang
 * @since 2024-04-13
 */
@Service
public class OuterAccountServiceImpl implements IOuterAccountService {

    @Resource
    private OuterAcountMapper mapper;

    @Override
    public List<OuterAccount> getList(Integer page, Integer size){
        QueryWrapper<OuterAccount> qwrapper = new QueryWrapper<>();
        IPage<OuterAccount> iPage = mapper.selectPage(new Page<>(page,size),qwrapper);
        return iPage.getRecords();
    }

    @Override
    public OuterAccount getItem(Integer id){
        return mapper.selectById(id);
    }

    @Override
    public boolean save(OuterAccount item){
        if (item.getId() == null)
            item.setId(0);
        if( item.getId() > 0 ){
            return mapper.updateById(item) > 0;
        }else{
            return mapper.insert(item) > 0;
        }

    }

    @Override
    public boolean delete(Integer id){
        return mapper.deleteById(id) > 0;
    }

    public List<OuterAccount> search(Integer id) {
        QueryWrapper<OuterAccount> qwrapper = new QueryWrapper<>();
        qwrapper.eq("account_id",id);
        return mapper.selectList(qwrapper);
    }

    @Override
    public List<OuterAccount> getListByAccountId(Integer accountId) {
        QueryWrapper<OuterAccount> qwrapper = new QueryWrapper<>();
        qwrapper.eq("account_id",accountId);
        return mapper.selectList(qwrapper);
    }
}
